package com.link.base.base.sms.service;

import com.link.base.sms.model.Signature;
import com.link.base.sms.model.SmsSend;
import com.link.base.base.accnt.dao.mybatis.mapper.AccountMapper;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.accnt.service.AccountService;
import com.link.base.base.sms.dao.mybatis.mapper.SendResultMapper;
import com.link.base.sms.model.SendResult;
import com.link.base.base.sms.dao.mybatis.mapper.SignatureMapper;
import com.link.base.base.sms.dao.mybatis.mapper.SmsSendMapper;
import com.link.base.base.sms.model.SmsBack;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: liulu
 * @Date:15:37 2018/11/5
 */
@Service
public class SendResultServiceImpl extends BasicServiceImpl<SendResult> implements SendResultService {

    @Resource
    private SendResultMapper sendResultMapper;

    @Resource
    private AccountService accountService;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private SignatureMapper signatureMapper;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    @Resource
    private SmsSendMapper smsSendMapper;

    @Override
    public BasicMapper<SendResult> getBasicMapper() {
        return sendResultMapper;
    }

    /**
     * @author liulu
     * @zhushi:据smsid查数据
     */
    @Override
    public List<SendResult> queryBysmsId(Long smsId) {

        List<SendResult> queryBysmsId = null;
        try {
            queryBysmsId = sendResultMapper.queryBysmsId(smsId);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LCLogger.withException(e);
        }
        return queryBysmsId;
    }

    @Override
    public void updateSmsId(SendResult sendResult) throws Exception {
        sendResultMapper.updateSmsId(sendResult);
    }

    @Override
    public void smsback(SmsBack smsBack) throws Exception {
        // 查询短信签名对应账套
        Signature signature = new Signature();
        signature.setText(smsBack.getSign());
        List<Signature> list = signatureMapper.querySign(signature);
        int signSize = list.size();
        if (signSize == 1) {
            signature = list.get(0);
        } else {
            throw new ServiceException("SMS-003", smsBack.getSign());
        }
        // 回复退订标识
        if (StringUtils.equals(smsBack.getText(), "T") || StringUtils.equals(smsBack.getText(), "TD")) {
            Account account = new Account();
            account.setMobilePhone1(smsBack.getMobile());
            account.setPageFlag(false);
            account.setLoginCorpId(signature.getCorpid());
            List<Account> accountList = accountService.queryByExamplePage(account);
            int len = accountList.size();
            // 标记消费者退订短信服务
            for (int i = 0;i < len;i++) {
               // accountList.get(i).setUnsubscribeSms("Y");
                accountMapper.updateUnSubSms(accountList.get(i));
            }
        }
        // 消息存储
        SmsSend smsSend = new SmsSend();
        smsSend.setMsg(smsBack.getText());
        smsSend.setExtend(smsBack.getExtend());
        smsSend.setSendState("Succeeded");
        smsSend.setSendMode("Imidiately");
        smsSend.setSigText(smsBack.getSign());
        Long receptionId = keyGenerateDao.keyGenerate();
        smsSend.setId(receptionId);
        smsSend.interfaceCreateData(signature.getCreatedBy(), signature.getCreatedBy(), signature.getCorpid());
        smsSendMapper.smsSendInsertByInterface(smsSend);

        SendResult sendResult = new SendResult();
        sendResult.setSmsId(receptionId);
        sendResult.setMobile(smsBack.getMobile());
        sendResult.setRequestStatus("SUCCESS");
        sendResult.setReportStatus("SUCCESS");
        sendResult.setExt("smsBack");
        Long backId = keyGenerateDao.keyGenerate();
        sendResult.setId(backId);
        sendResult.interfaceCreateData(signature.getCreatedBy(), signature.getCreatedBy(), signature.getCorpid());
        sendResultMapper.sendResultInsertByInterface(sendResult);
    }

    @Override
    public List<SendResult> queryByMobileExamplePage(SendResult sendResult) throws Exception {
        Account account = new Account();
        account.setId(sendResult.getAccountId());
        account = accountMapper.queryById(account);
        sendResult.setMobile(account.getMobilePhone1());
        return sendResultMapper.queryByMobileExamplePage(sendResult);
    }
}
